import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table} from 'antd';
import {TABLE, DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSIONS} from './constants';
import {hasPermissions} from './utils';
import routes from './routes';
const {Description} = DescriptionList;
import {injectIntl} from './intl';
import {lcKind, lcStatus, currency, partOrderPriceTerms} from '../Enum';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import styles from './style.css';

export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }

    render() {
        const {formatMessage} = this.props.intl;
        const Yes = formatMessage({
            id: 'detailPage.yes',
            defaultMessage: '是'
        });
        const No = formatMessage({
            id: 'detailPage.no',
            defaultMessage: '否'
        });
        const details = this.props.detail.details || [];
        const columns = [
            {
                title: formatMessage({
                    id: 'detailPage.date',
                    defaultMessage: 'Date'
                }),
                dataIndex: 'date',
                render: text => formatDateTime(text, DATE_FORMAT)
            },
            {
                title: formatMessage({
                    id: 'detailPage.out/In',
                    defaultMessage: 'Out/In'
                }),
                dataIndex: 'outOrIn'
            },
            {
                title: formatMessage({
                    id: 'detailPage.code',
                    defaultMessage: 'LC No'
                }),
                dataIndex: 'code'
            },
            {
                title: formatMessage({
                    id: 'detailPage.partInvoiceCode',
                    defaultMessage: 'Invoice No'
                }),
                dataIndex: 'partInvoiceCode',
            },
            {
                title: formatMessage({
                    id: 'detailPage.amount',
                    defaultMessage: 'Amount'
                }),
                dataIndex: 'amount',
                render: text => formatAmount(text)
            },
            {
                title: formatMessage({
                    id: 'detailPage.actualDepartedDate',
                    defaultMessage: 'BL Date'
                }),
                dataIndex: 'actualDepartedDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            },
            {
                title: formatMessage({
                    id: 'detailPage.partOrderPriceTerms',
                    defaultMessage: 'Term'
                }),
                dataIndex: 'partOrderPriceTerms',
                render: text => conventEnumValueToString(partOrderPriceTerms, text)
            },
            {
                title: formatMessage({
                    id: 'detailPage.cashInDate',
                    defaultMessage: 'Payment Received Date'
                }),
                dataIndex: 'cashInDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            }
        ];

        const operations = [];
        const {options = []} = this.props.detail;
        if(hasPermissions(this.props.permissions, PERMISSIONS.receive) && hasPermissions(options, PERMISSIONS.receive))
            operations.push(<Button key="receive" onClick={this.props.handleReceiveClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.receive',
                    defaultMessage: '到款登记'
                })
            }</Button>);
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '信用证信息详情'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{this.props.detail.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.branchCode',
                            defaultMessage: '营销分公司编号'
                        })}>{this.props.detail.branchCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.branchName',
                            defaultMessage: '营销分公司名称'
                        })}>{this.props.detail.branchName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{this.props.detail.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{this.props.detail.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '信用证号码'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issueCountry',
                            defaultMessage: '开证国家'
                        })}>{this.props.detail.issueCountry}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issueBank',
                            defaultMessage: '开证银行'
                        })}>{this.props.detail.issueBank}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.sendBank',
                            defaultMessage: '寄出银行'
                        })}>{this.props.detail.sendBank}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcPayMentDays',
                            defaultMessage: '信用证账期'
                        })}>{this.props.detail.lcPayMentDays}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issuePrice',
                            defaultMessage: '开证金额'
                        })}>{this.props.detail.issuePrice}</Description>
                        <Description term={formatMessage({            
                            id: 'detailPanel.text.issueDate',
                            defaultMessage: '开证日期'
                        })}>{formatDateTime(this.props.detail.issueDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lastLoadDate',
                            defaultMessage: '最迟装船日期'
                        })}>{formatDateTime(this.props.detail.lastLoadDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.validityDate',
                            defaultMessage: '有效期'
                        })}>{this.props.detail.validityDate}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcKind',
                            defaultMessage: '信用证类型'
                        })}>{conventEnumValueToString(lcKind, this.props.detail.lcKind)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcStatus',
                            defaultMessage: '信用证状态'
                        })}>{conventEnumValueToString(lcStatus, this.props.detail.lcStatus)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, this.props.detail.currency)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.isBatches',
                            defaultMessage: '是否分批'
                        })}>{this.props.detail.isBatches ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.amount',
                            defaultMessage: '件数'
                        })}>{this.props.detail.amount}</Description>
                    </DescriptionList>
                </Card>
                <Card title={formatMessage({
                    id: 'detailPage.data.details',
                    defaultMessage: '信用证清单列表'
                })}>
                    <Table
                        className="white-space-nowrap"
                        dataSource={details}
                        rowKey="id"
                        pagination={false}
                        {...TABLE}
                        columns={columns}/>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    handleReceiveClick: () => {
        props.history.push(routes.receive.format(props.id));
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
